/**
 * author      : dormi330
 * date        : 2018/6/26
 * project     : mybatis
 * description : 订单包裹信息, 与订单TradeOrder 1-N 关系
 */

package com.deep.trade.domain.order;

import com.deep.trade.common.BaseEntity;
import com.deep.trade.infrastructure.utils.CommonUtils;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class OrderParcel extends BaseEntity {

    public static String STATUS_ADD = "parcel_added";
    public static String STATUS_DELIVERYED = "parcel_deliveryed";

    public class ParcelItemDetail {
        private String name;                // 名称
        private String skuId;               //
        private String tradeOrderSkuId;     //
        private int quantity;               // 包裹中该skuId的数量

        public ParcelItemDetail(String name, String skuId, String tradeOrderSkuId, int quantity) {
            this.name = name;
            this.skuId = skuId;
            this.tradeOrderSkuId = tradeOrderSkuId;
            this.quantity = quantity;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getSkuId() {
            return skuId;
        }

        public void setSkuId(String skuId) {
            this.skuId = skuId;
        }

        public String getTradeOrderSkuId() {
            return tradeOrderSkuId;
        }

        public void setTradeOrderSkuId(String tradeOrderSkuId) {
            this.tradeOrderSkuId = tradeOrderSkuId;
        }

        public int getQuantity() {
            return quantity;
        }

        public void setQuantity(int quantity) {
            this.quantity = quantity;
        }
    }

    private String tradeOrderId;            // 交易订单号
    private String parcelItemJson;          // 包裹中的东西, 使用 string-json 存储
    private String status;                  // 物流状态

    private String trackingNo;              // 物流单号
    private String trackingNoImg;           // 物流图片, 目前可选
    private String logisticsCompanyName;    // 投递公司名
    private String logisticsCompanyCode;    // 投递公司编码


    // 非持久化字段
    private List<ParcelItemDetail> parcelItemList;  // 包裹里面的内容

    public void initStartStatus() {
        this.status = STATUS_ADD;
    }

    public void addParcelItem(TradeOrderSku orderSku, int qty) {
        if (null == parcelItemList) parcelItemList = new ArrayList<>();
        ParcelItemDetail parcelItem = new ParcelItemDetail(orderSku.getItemName(), orderSku.getSkuId(), orderSku.getId(), qty);
        parcelItemList.add(parcelItem);
    }

    public OrderParcel packToString() {
        if (null == parcelItemList || parcelItemList.size() == 0) {
            parcelItemList = new ArrayList<>(0);
        }

        List<ParcelItemDetail> list = parcelItemList.stream().collect(Collectors.toList());
        this.parcelItemJson = CommonUtils.objectToJson(list);
        return this;
    }

    public void unpackFromString() {
        if (null == parcelItemJson) {
            parcelItemList = new ArrayList<>(0);
        } else {
            parcelItemList = new Gson().fromJson(parcelItemJson, List.class);
        }
    }

    // === setter / getter ===

    public String getTradeOrderId() {
        return tradeOrderId;
    }

    public void setTradeOrderId(String tradeOrderId) {
        this.tradeOrderId = tradeOrderId;
    }

    public String getParcelItemJson() {
        return parcelItemJson;
    }

    public void setParcelItemJson(String parcelItemJson) {
        this.parcelItemJson = parcelItemJson;
    }

    public String getTrackingNo() {
        return trackingNo;
    }

    public void setTrackingNo(String trackingNo) {
        this.trackingNo = trackingNo;
    }

    public String getTrackingNoImg() {
        return trackingNoImg;
    }

    public void setTrackingNoImg(String trackingNoImg) {
        this.trackingNoImg = trackingNoImg;
    }

    public List<ParcelItemDetail> getParcelItemList() {
        return parcelItemList;
    }

    public void setParcelItemList(List<ParcelItemDetail> parcelItemList) {
        this.parcelItemList = parcelItemList;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getLogisticsCompanyName() {
        return logisticsCompanyName;
    }

    public void setLogisticsCompanyName(String logisticsCompanyName) {
        this.logisticsCompanyName = logisticsCompanyName;
    }

    public String getLogisticsCompanyCode() {
        return logisticsCompanyCode;
    }

    public void setLogisticsCompanyCode(String logisticsCompanyCode) {
        this.logisticsCompanyCode = logisticsCompanyCode;
    }
}
